package com.wteam.dragon.shop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.pojo.bean.Coupon;
import com.wteam.dragon.shop.pojo.vo.CouponVo;
import com.wteam.dragon.shop.service.CouponService;
import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2021/3/24 21:35
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "优惠卷管理")
@RequestMapping("coupon")
public class CouponController {
    private final CouponService couponService;

    @Log("添加优惠卷")
    @ApiOperation("添加优惠卷")
    @PostMapping("addFlashPromotion")
    @PreAuthorize("@el.check('addFlashPromotion')")
    public ResultMessage addFlashPromotion(CouponVo couponVo, @RequestParam List<Long> productSortIds, @RequestParam List<Long> productIds){
        Coupon coupon = BeanUtil.from(couponVo, Coupon.class);
        return couponService.addCoupon(coupon, productSortIds, productIds);
    }

    @Log("删除优惠卷")
    @ApiOperation("删除优惠卷")
    @DeleteMapping("delCoupon")
    @PreAuthorize("@el.check('delCoupon')")
    public ResultMessage delCoupon(Long id){
        return couponService.delCoupon(id);
    }

    @Log("更改优惠卷")
    @ApiOperation("更改优惠卷")
    @PutMapping("editCoupon")
    @PreAuthorize("@el.check('editCoupon')")
    public ResultMessage editCoupon(CouponVo couponVo, @RequestParam List<Long> productSortIds, @RequestParam List<Long> productIds){
        Coupon coupon = BeanUtil.from(couponVo, Coupon.class);
        return couponService.editCoupon(coupon, productSortIds, productIds);
    }

    @Log("分页获得优惠卷")
    @ApiOperation("分页获得优惠卷")
    @GetMapping("pageCoupon")
    @PreAuthorize("@el.check('pageCoupon')")
    public ResultMessage pageCoupon(Page<Coupon> page, String name, Integer type){
        return couponService.pageCoupon(page, name, type);
    }

    @Log("获得优惠卷详情")
    @ApiOperation("获得优惠卷详情")
    @GetMapping("getCoupon")
    @PreAuthorize("@el.check('getCoupon')")
    public ResultMessage getCoupon(Long id){
        return couponService.getCoupon(id);
    }

    @Log("领取优惠卷")
    @ApiOperation("领取优惠卷")
    @PutMapping("receiveCoupon")
    @PreAuthorize("@el.check('receiveCoupon')")
    public ResultMessage receiveCoupon(Long id){
        return couponService.receiveCoupon(id);
    }

}
